package com.hbnu.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.CommonDataSource;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;

@Configuration  //表示当前这个类是个配置类
@ComponentScan("com.hbnu") //包扫描配置，扫描指定位置下面的所有类或接口
@EnableAspectJAutoProxy //开启AOP自动代理
@EnableTransactionManagement //开启事务管理
public class ApplicationConfig {

    @Bean  //表示将方法的返回值对象交给Spring容器管理
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();

        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/class2109?serverTimezone=GMT&amp;useSSL=false");
        dataSource.setUser("root");
        dataSource.setPassword("123456");

        return dataSource;

    }

    @Bean
    public JdbcTemplate getJdbcTemplate() throws PropertyVetoException {
        return new JdbcTemplate(getDataSource());
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException {
        return new DataSourceTransactionManager(getDataSource());
    }


}















